package com.wgn.reportquery.service;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.cyberneko.html.parsers.DOMParser;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

import com.wgn.reportquery.cons.Const;
import com.wgn.reportquery.dao.ImportDaoImple;
import com.wgn.reportquery.model.Report;
import com.wgn.reportquery.model.Reportdata;
import com.wgn.reportquery.utils.Importer;
import com.wgn.reportquery.utils.UnzipUtils;

public class ImportService {

/*	public String importFile(String fileName, InputStream inputStream,
			String username) {
		// TODO Auto-generated method stub
		String msg = "ok";
		// 读取数据
		//
		Importer im = new Importer();
		ImportDaoImple importDaoImple = new ImportDaoImple();

		{
			try {
				{
					List<Reportdata> datas = im.getData(inputStream,
							importDaoImple, fileName);

					importDaoImple.insert(datas, username);
				}

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				msg = e.getMessage();
			}

		}
		importDaoImple.close();
		return msg;
	}

	private static void testGetDate() {
		// TODO Auto-generated method stub
		// 创建一个解析器
		DOMParser parser = new DOMParser();
		// 解析HTML文件
		try {
			parser.parse("file:///d:/work/NekoHTMLTest/test/检验报告单.htm");// 获取解析后的DOM树
			Node node = parser.getDocument();
			Report r = Importer.getReport(node);
			String date = r.getDate();
			System.out.println(date);
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private static void testFileRename() throws IOException {
		File file = new File("test/oldxxx.htm");
		String oldFileNmae = file.getName();
		String pathWithName = file.getAbsolutePath();
		String path = pathWithName.substring(0,
				pathWithName.indexOf(oldFileNmae));
		File tmpFile = new File(path + "/" + "20170306G0561088" + ".htm");
		if (tmpFile.exists()) {
			tmpFile.delete();
			System.out.println("删除重复的文件" + tmpFile.getName());
		}

		file.renameTo(tmpFile);
		System.out.println(oldFileNmae + "更名为" + tmpFile.getName());
	}

	public String importFileRar(String fileName, InputStream inputStream,
			String username) throws Exception {
		// TODO Auto-generated method stub

		// TODO Auto-generated method stub

		// TODO Auto-generated method stub
		String msg = "ok";
		// 读取数据
		File tarFile = UnzipUtils.inputstream2File(inputStream, Const.TMPDIR
				+ File.separator + fileName);
		// // 解压
		UnzipUtils.untar(tarFile.getAbsolutePath(), Const.TMPDIR
				+ File.separator + username);
		Importer im = new Importer();
		ImportDaoImple importDaoImple = new ImportDaoImple();
		File[] files = (new File(Const.TMPDIR + File.separator + username))
				.listFiles();
		for (File file : files) {
			try {
				{
					List<Reportdata> datas = im.getData(file, importDaoImple);

					importDaoImple.insert(datas, username);
				}

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				msg = e.getMessage();
			}

		}
		importDaoImple.close();
		if (tarFile.exists())
			tarFile.delete();
		return msg;

	}

	public String importFileZip(String fileName, InputStream inputStream,
			String username) throws Exception {
		// TODO Auto-generated method stub

		// TODO Auto-generated method stub

		// TODO Auto-generated method stub
		String msg = "ok";
		// 读取数据
		File tmpFile = UnzipUtils.inputstream2File(inputStream, Const.TMPDIR
				+ File.separator + fileName);
		// // 解压
		List<InputStream> ins = UnzipUtils.unzip(tmpFile.getAbsolutePath());
		Importer im = new Importer();
		ImportDaoImple importDaoImple = new ImportDaoImple();
		for (InputStream in : ins) {
			try {
				{
					List<Reportdata> datas = im.getData(in, importDaoImple,
							fileName);

					importDaoImple.insert(datas, username);
				}

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				msg = e.getMessage();
			}

		}
		importDaoImple.close();
		if (tmpFile.exists())
			tmpFile.delete();
		return msg;

	}*/
}
